/**
 * 
 */
package com.elephantgreen.xml;

import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.elephantgreen.common.ElephantgreenCommon;

/**
 * @author ABC
 * 
 */
public abstract class ElephantgreenXmlHandler {

	private File xmlFile;
	private NodeList nodes;
	private Document doc;

	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private ElephantgreenXmlHandler() {

	}

	public ElephantgreenXmlHandler(String fName, String parentNodeName) {

		try {
			xmlFile = new File(fName);
			nodes = getParentNodeList(parentNodeName);
		} catch (NullPointerException npe) {
			System.err.print("File Not Found : " + fName);
			npe.printStackTrace();
		}

	}

	public NodeList getParentNodeList(String parentNodeName) {

		NodeList nl = null;

		try {

			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();

			nl = doc.getElementsByTagName(parentNodeName);

		} catch (ParserConfigurationException pce) {

			pce.printStackTrace();
		} catch (SAXException se) {

			se.printStackTrace();
		} catch (IOException ioe) {

			ioe.printStackTrace();
		}

		return nl;

	}

	public abstract Vector<ElephantgreenCommon> getItems();

	public File getXmlFile() {
		return xmlFile;
	}

	public void setXmlFile(File xmlFile) {
		this.xmlFile = xmlFile;
	}

	public NodeList getNodes() {
		return nodes;
	}

	public void setNodes(NodeList nodes) {
		this.nodes = nodes;
	}

	public Document getDocumnet() {
		return doc;
	}

}
